<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../plugin/xterm/xterm.css" />
    <script src="../../js/load.js"></script>
    <script src="../../js/webssh.js?v=1.0.0"></script>
    <script src="../../plugin/xterm/xterm.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div id="terminal"></div>>
    <div id="opcontainer">
        <button class="layui-btn" onclick="HandleClose()">关闭</button>
    </div>
</div>
<script>
    var connectData = window.localStorage.getItem("connectData");
    openTerminal(JSON.parse(connectData));
    function openTerminal(options){
        var client = new WSSHClient();
        var term = new Terminal({
            cols: 97,
            rows: 50,
            cursorBlink: true, // 光标闪烁
            cursorStyle: "block", // 光标样式  null | 'block' | 'underline' | 'bar'
            scrollback: 10000, //回滚
            tabStopWidth: 8, //制表宽度
            screenKeys: true
        });

        term.on('data', function (data) {
            //键盘输入时的回调函数
            client.sendClientData(data);
        });
        term.open(document.getElementById('terminal'));
        //在页面上显示连接中...
        term.write('Connecting...\r\n');
        //执行连接操作
        client.connect({
            onError: function (error) {
                //连接失败回调
                term.write('Error: ' + error + '\r\n');
            },
            onConnect: function () {
                //连接成功回调
                client.sendInitData(options);
            },
            onClose: function () {
                //连接关闭回调
                term.write("\rconnection closed");
                window.location.href = GetLocalRootPath()+"pages/webssh/config.html";
            },
            onData: function (data) {
                //收到数据时回调
                if (data.indexOf('\r\nlogout')>-1){
                    window.localStorage.clear();
                    window.location.href = GetLocalRootPath()+"pages/webssh/config.html";
                }
                term.write(data);
            }
        });
    }

    function HandleClose(){
         window.location.href = GetLocalRootPath()+"pages/webssh/config.html";
    }
</script>
</body>
